<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="workstationmachineList">
      <el-table-column label="设备编号" align="center" prop="machineryCode" />
      <el-table-column label="设备名称" align="center" width="150px" prop="machineryName" />
      <el-table-column label="数量" width="50px" align="center" prop="quantity" />
      <el-table-column label="操作" v-if="optType !='view'" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workstation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup class="Workstationmachine">
import { listWorkstationmachine, getWorkstationmachine, delWorkstationmachine, addWorkstationmachine, updateWorkstationmachine } from "@/api/mes/md/workstationmachine";

const { proxy } = getCurrentInstance();

const loading = ref(true);// 遮罩层
const ids = ref([]); // 选中数组
const showSearch = ref(true);// 显示搜索条件
const single = ref(true);// 非单个禁用
const multiple = ref(true);// 非多个禁用
const total = ref(0);// 总条数
const title = ref(""); // 弹出层标题
const open = ref(false);// 是否显示弹出层

const workstationmachineList = ref([]); // 设备资源表格数据

const data = reactive({
  form: {},// 表单参数
 // 查询参数
 queryParams: {
        pageNum: 1,
        pageSize: 10,
        workstationId: null,
        machineId: null,
        machineCode: null,
        machineName: null,
        quantity: null,
      }

});

const { queryParams, form, rules } = toRefs(data);//对象解构

/** 查询设备资源列表 */
function getList() {
  loading.value = true;
  queryParams.value.workstationId = this.workstationId;
  listWorkstationmachine(queryParams.value).then(response => {
    workstationmachineList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 表单重置
function reset() {
  form.value = {
    recordId: null,
    workstationId: null,
    machineId: null,
    machineCode: null,
    machineName: null,
    quantity: null,
    remark: null,
    attr1: null,
    attr2: null,
    attr3: null,
    attr4: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("form");
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId)
  single.value = selection.length!==1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备资源";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["form"].validate(valid => {
    if (valid) {
      if (form.value.recordId != null) {
        updateWorkstationmachine(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWorkstationmachine(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const recordIds = row.recordId || ids.value;
  proxy.$modal.confirm('是否确认删除设备资源？').then(function() {
    return delWorkstationmachine(recordIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}




  // props: {
  //     workstationId: undefined,
  //     optType: undefined,
  // }

  getList();


</script>
